/** 
 * File-Name:UserAction.java
 *
 * Created on 2011-6-2 下午10:22:05
 * 
 * @author: Neo (neolimeng@gmail.com)
 * Software Engineering Institute, Peking University, China
 * 
 * Copyright (c) 2009, Peking University
 * 
 *
 */
package site.web;

import java.util.List;

import neoutil.CommonAction;
import neoutil.CommonService;
import neoutil.Page;
import net.sf.json.util.JSONStringer;

import org.apache.log4j.Logger;

import site.entity.Category;
import site.entity.User;
import site.service.UserRepository;

/**
 * Description:
 * 
 * @author: Neo (neolimeng@gmail.com) Software Engineering Institute, Peking
 *          University, China
 * @version 1.0 2011-6-2 下午10:22:05
 */
public class UserAction extends CommonAction {

	private static final long serialVersionUID = -3463954058240251645L;
	private static Logger log = Logger.getLogger(UserAction.class);
	private User user;
	private String email;
	private String password;
	private Page<User> page = new Page<User>(9);
	private List<Category> categories;

	public String login() {
		try {
			email = getRequestParameter("email");
			password = getRequestParameter("password");
			if (null == email || null == password || "".equals(email)
					|| "".equals(password)) {

				jsonResult = new JSONStringer().object().key("result")
						.value("error").key("tip").value("登录信息错误，请重新输入。")
						.endObject().toString();
			} else {
				UserRepository userRepository = getUserRepository();
				user = userRepository.login(email, password);
				if (user == null) {
					jsonResult = new JSONStringer().object().key("result")
							.value("error").key("tip")
							.value("登录信息错误，或账户未激活，请重试或联系网站管理员。").endObject()
							.toString();
				} else {
					setSessionObject("loginUser", user);

					jsonResult = new JSONStringer().object().key("result")
							.value("success").key("tip")
							.value("登录成功，系统将自动转到首页...").endObject().toString();
				}
				log.info(jsonResult);
			}
		} catch (Exception e) {
			e.printStackTrace();
			jsonResult = new JSONStringer().object().key("result")
					.value("exception").key("tip").value("注册过程中出现异常，请重试。")
					.endObject().toString();
		}
		return SUCCESS;
	}

	public String logout() {
		try {
			setSessionObject("loginUser", null);
			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return INPUT;
		}
	}

	public String signUp() {
		try {
			String name = getRequestParameter("name");

			String password = getRequestParameter("password");
			String reenterPassword = getRequestParameter("reenterPassword");
			String address = getRequestParameter("address");
			String description = getRequestParameter("description");
			String fax = getRequestParameter("fax");
			String rid = getRequestParameter("rid");
			String telephone = getRequestParameter("telephone");

			// System.out.println("NAME:: " + name + "\t" + password + "\t"
			// + reenterPassword + "\t" + rid);
			if (name == null || rid == null || rid.trim().equals("")
					|| name.trim().equals("") || password == null
					|| password.trim().equals("") || reenterPassword == null
					|| reenterPassword.trim().equals("")
					|| !reenterPassword.trim().equals(password.trim())) {
				jsonResult = new JSONStringer().object().key("result")
						.value("error").key("tip").value("填写信息有误，请重新填写。")
						.endObject().toString();
			} else {
				UserRepository userRepository = getUserRepository();
				User user = new User();
				user.setName(name);
				user.setPassword(password);
				user.setAddress(address);
				user.setDescription(description);
				user.setEmail(email);
				user.setFax(fax);
				user.setRid(Integer.parseInt(rid));
				user.setTelephone(telephone);

				String result = userRepository.saveUser(user);
				if (CommonService.SUCCESS.equals(result)) {
					jsonResult = new JSONStringer().object().key("result")
							.value("success").key("tip")
							.value("注册成功，系统将自动跳转到登录页面...").endObject()
							.toString();
				} else if (UserRepository.USER_EXIST == result) {
					jsonResult = new JSONStringer().object().key("result")
							.value("error").key("tip")
							.value("Email已经被占用，请更换Email。").endObject()
							.toString();
				} else {
					jsonResult = new JSONStringer().object().key("result")
							.value("error").key("tip").value("注册过程中出现错误，请重试。")
							.endObject().toString();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			jsonResult = new JSONStringer().object().key("result")
					.value("exception").key("tip").value("注册过程中出现异常，请重试。")
					.endObject().toString();
		}
		System.out.println(jsonResult);
		return SUCCESS;
	}

	public String getUnactivatedUsers() {
		try {
			page = getUserRepository().getUnactivatedUsers(page);

			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}

	public String activateUser() {
		try {
			String uuid = getRequestParameter("uuid");
			getUserRepository().activateUser(uuid);

			return SUCCESS;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public List<Category> getCategories() {
		return categories;
	}

	public void setCategories(List<Category> categories) {
		this.categories = categories;
	}

	public Page<User> getPage() {
		return page;
	}

	public void setPage(Page<User> page) {
		this.page = page;
	}

}
